using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using EM.Repository.utility;


namespace EM.Common
{
    public partial class TopMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int PageId = 0;

            if (Request.QueryString["pgid"] != null)
            {
                PageId = Convert.ToInt32(Request.QueryString["pgid"]);
            }

            string APPROOT = "";
            AppPathFinder AppPath = new AppPathFinder();
            APPROOT = AppPath.GetApplicationPath();

            string sSQL = "";
            sSQL += " SELECT P.PAGEID, MENUNAME, PARENTPAGEID, PAGETYPE,";
            sSQL += " CASE WHEN PAGETYPE = 'M' AND PARENTPAGEID IS NOT NULL THEN '" + APPROOT + "/SubMenu.aspx?pgid=' + convert(nvarchar,P.PAGEID) ELSE '" + APPROOT + "/' + PAGEURL + '?pgid=' + convert(nvarchar,P.PAGEID) END AS PAGEURL ";
            sSQL += " FROM TBLPAGE P, TBLPAGE_L L";
            sSQL += " WHERE P.PAGEID = L.PAGEID";
            sSQL += " AND P.STATUS = 'A'";
            sSQL += " AND L.LANGCODE = @LANGCODE ";
            sSQL += " AND GETDATE() BETWEEN STARTDATE AND DATEADD(\"d\", 1, ISNULL(ENDDATE,'31/12/2099'))";
            sSQL += " AND DISPLAYMENUFLG = 'Y' ";
            sSQL += " AND PAGETYPE <> 'SF'";
            sSQL += " AND P.PAGEID IN (SELECT PAGEID FROM TBLROLEPAGE_COMPILED WHERE ROLEID IN (SELECT ROLEID FROM TBLUSERROLE WHERE USERID = @USERID AND GETDATE() BETWEEN STARTDATE AND DATEADD(d,1, ISNULL(ENDDATE,CONVERT(DATETIME,\'30/12/2099\')))))";
            sSQL += " ORDER BY PAGEORDER";

            DataTable dtTopMenu = new DataTable();
            DbHelper oDataSrv = new DbHelper();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sSQL;
            cmd.Parameters.Add("LANGCODE", SqlDbType.NVarChar).Value = Session["LANGCODE"].ToString();
            cmd.Parameters.Add("USERID", SqlDbType.NVarChar).Value = Session["USERID"].ToString();

            dtTopMenu = oDataSrv.GetDataTable(cmd, "TBLMENU");

            tsTopMenu.DataTextField = "MENUNAME";
            tsTopMenu.DataValueField = "PAGEID";
            tsTopMenu.DataFieldID = "PAGEID";
            tsTopMenu.DataFieldParentID = "PARENTPAGEID";
            tsTopMenu.DataNavigateUrlField = "PAGEURL";
            tsTopMenu.DataSource = dtTopMenu;
            tsTopMenu.DataBind();

            if (PageId > 0)
            {
                sSQL = " ";
                sSQL += " SELECT PARENTPAGEID";
                sSQL += " FROM TBLPAGE";
                sSQL += " WHERE PAGEID = @PAGEID";
                cmd = new SqlCommand();
                cmd.CommandText = sSQL;
                cmd.Parameters.Add("@PAGEID", SqlDbType.Int).Value = PageId;

                string PARENTPAGEID = oDataSrv.ExecuteScalar(cmd);


                //tsTopMenu.FindTabByValue(PARENTPAGEID).Selected = true;
                //tsTopMenu.FindTabByValue(PageId.ToString()).Selected = true;
            }

            if (Session["MAINMENUFLG"] == null)
            {
                Session.Add("MAINMENUFLG", 1);
                tsTopMenu.FindTabByValue("1").Selected = true;
                tsTopMenu.FindTabByValue("2").Selected = true;
            }
        }
    }
}